const loadGruntTasks = require("load-grunt-tasks");
const minimist = require("minimist");
const sass = require("node-sass");
const data = {
  menus: [
    {
      name: "Home",
      icon: "aperture",
      link: "index.html",
    },
    {
      name: "Features",
      link: "features.html",
    },
    {
      name: "About",
      link: "about.html",
    },
    {
      name: "Contact",
      link: "#",
      children: [
        {
          name: "Twitter",
          link: "https://twitter.com/w_zce",
        },
        {
          name: "About",
          link: "https://weibo.com/zceme",
        },
        {
          name: "divider",
        },
        {
          name: "About",
          link: "https://github.com/zce",
        },
      ],
    },
  ],
  pkg: require("./package.json"),
  date: new Date(),
};

let config = {
  public: "public",
  src: "src",
  temp: "temp",
  dist: "dist",
  paths: {
    fonts: "assets/fonts/*",
    images: "assets/images/*",
    pages: "**/*.html",
    scripts: "assets/scripts/**.js",
    styles: "assets/styles/**.scss",
    css: "assets/styles/**.css",
  },
};

module.exports = (grunt) => {
  grunt.initConfig({
    // grunt-swigtemplates
    swigtemplates: {
      options: {
        defaultContext: {
          data: data,
        },
        templatesDir: config.src,
      },
      // 没找到对应的忽略 配置
      production: {
        dest: config.temp,
        src: [`${config.src}/*.html`],
      },
    },
    // grunt-babel @babel/core @babel/preset-env
    babel: {
      options: {
        sourceMap: '<%= isProd %>',
        presets: ["@babel/preset-env"],
      },
      temp: {
        // 开启拓展名
        expand: true,
        dest: `${config.temp}/`,
        cwd: config.src,
        src: [`${config.paths.scripts}`],
      },
    },
    // node-sass grunt-sass
    sass: {
      options: {
        implementation: sass,
        sourceMap: '<%= isProd %>',
      },
      dist: {
        expand: true,
        ext: ".css",
        dest: `${config.temp}/`,
        cwd: config.src,
        src: [`${config.paths.styles}`],
      },
    },
    // grunt-contrib-imagemin gifsicle 
    imagemin: {
      images: {
        files: [{
          expand: true,
          cwd: config.src,
          src: [`${config.paths.images}`],
          dest: config.dist
        }]
      },
      fonts: {
          files: [{
              expand: true,
              cwd: config.src,
              src: [`${config.paths.fonts}`],
              dest: config.dist
          }]
      }
    },
    // grunt-contrib-copy
    copy: {
      public: {
        files: [{
          expand: true,
          cwd: config.public,
          flatten: true,
          src: `**`,
          dest: config.dist,
          filter: 'isFile'
        }]
      },
      html: {
        expand: true,
        cwd: config.temp,
        src: [config.paths.pages],
        dest: config.dist,
      }
    },
    uglify: {
      options: {
        mangle: {
          reserved: ['jQuery']
        }
      },
      temp: {
        files: {
          'dist/assets/scripts/vendor.js' : [
                './node_modules/jquery/dist/jquery.js', 
                './node_modules/popper.js/dist/umd/popper.js',
                './node_modules/bootstrap/dist/js/bootstrap.js'
              ]
        }
        // [{
        //   expand: true,
        //   cwd: config.temp,
        //   src: [
        //     '../node_modules/jquery/dist/jquery.js', 
        //     '../node_modules/popper.js/dist/umd/popper.js',
        //     '../node_modules/bootstrap/dist/js/bootstrap.js'
        //   ],
        //   dest: `${config.dist}/assets/scripts/vendor.js`
        // }] 
      }
    },
    // grunt-contrib-cssmin
    cssmin: {
      options: {
        mergeIntoShorthands: false,
        roundingPrecision: -1
      },
      temp: {
        files: {
          'dist/assets/styles/vendor.css': './node_modules/bootstrap/dist/css/bootstrap.css'
        }
      }
    },
    // grunt-contrib-htmlmin
    htmlmin: {
      temp: {
        files: [{
          expand: true,
          cwd: config.temp,
          src: [config.paths.pages],
          dest: config.dist
        }] 
      }
    },
    // grunt-bytesize
    size: {
      files: {
        'dist': 'dist'
      }
    },
    // grunt-useref grunt-contrib-uglify  grunt-contrib-htmlmin 
    useref: {
      html: `${config.temp}/${config.paths.pages}`,
      temp: config.dist
    },
    // grunt-bytesize
    serve: {
      // grunt-browser-sync
      default: {
      },
    },
    build: {
      default: {

      }
    },
    start: {
      default: {

      }
    },
    // grunt-contrib-watch
    watch: {
      bulidHtml: {
        files: `${config.src}/${config.paths.pages}`,
        tasks: ["swigtemplates"],
      },
      bulidScss: {
        files: `${config.src}/${config.paths.styles}`,
        tasks: ["sass"],
      },
      bulidJs: {
        files: `${config.src}/${config.paths.scripts}`,
        tasks: ["babel"],
      },
      buildExtra: {
        files: [
          `${config.src}/${config.paths.images}`, 
          `${config.src}/${config.paths.fonts}`,
          config.public
        ],
        options: {
          reload: true
        }
      }
    },
    // grunt-browser-sync
    browserSync: {
      options: {
        port: '<%= port %>',
        open: '<%= open %>'
      },
      build: {
        notify: false,
        bsFiles: {
          src: [config.temp, config.src, config.public],
        },
        options: {
          server: {
            baseDir: [config.temp, config.src, config.public],
            routes: {
              "/node_modules": "node_modules",
            },
          },
        },
      },
      prod: {
        notify: false,
        bsFiles: {
          src: [config.dist],
        },
        options: {
          server: config.dist,
        },
      },
    },
    // grunt-gh-pages
    'gh-pages': {
      options: {
        base: 'dist',
        branch: '<%= branch %>'
      },
      src: '**/*'
    },
    // grunt-contrib-clean
    clean: [config.temp, config.dist],
  });
  loadGruntTasks(grunt);
  // 注册对应的任务 命令
  let port = grunt.option('port') || 2080
  let open = grunt.option('open') || false
  let branch = grunt.option('branch') || 'gh-pages'
  grunt.config.set('port', port)
  grunt.config.set('open', open)
  grunt.config.set('branch', branch)

  let isProd = process.env.NODE_ENV
  ? process.env.NODE_ENV === 'production'
  : grunt.option('production') || grunt.option('prod') || false
grunt.config.set('isProd', !isProd)

  // grunt-contrib-concat
  grunt.registerTask("lint", ['jshint', 'stylelint'])
  grunt.registerTask("compile", ['swigtemplates', 'babel', 'sass'])
  grunt.registerTask("serve", ['compile', 'browserSync'])
  grunt.registerTask("build", ['clean', 'compile', 'useref', 'concat', 'uglify', 'cssmin', 'htmlmin', 'imagemin', 'copy', 'size']);
  grunt.registerTask("start", ['build', 'browserSync:prod'])
  grunt.registerTask("deploy", ['build', 'gh-pages'])
};
